from owl_Api import *
from model.informationExtract import query,transfer2JSON

# 目标owl地址
document_owl_path = r"cantonowl\Document.owl"
event_owl_path = r"cantonowl\Event.owl"
personage_owl_path = r"cantonowl\Personage.owl"
location_owl_path = r"cantonowl\Location.owl"
time_owl_path = r"cantonowl\Time.owl"
relic_owl_path = r"cantonowl\Relic.owl"
recall_owl_path = r"cantonowl\Recall.owl"
literature_owl_path = r"cantonowl\Literature.owl"

# model built
owl_model = Parse_Owl()
document_dic, document_ind_start, document_ind_end = owl_model.get_individuals_dic(document_owl_path)
event_dic, event_ind_start, event_ind_end = owl_model.get_individuals_dic(event_owl_path)
personage_dic, personage_ind_start, personage_ind_end = owl_model.get_individuals_dic(personage_owl_path)
location_dic, location_ind_start, location_ind_end = owl_model.get_individuals_dic(location_owl_path)
time_dic, time_ind_start, time_ind_end = owl_model.get_individuals_dic(time_owl_path)
relic_dic, relic_ind_start, relic_ind_end = owl_model.get_individuals_dic(relic_owl_path)
recall_dic, recall_ind_start, recall_ind_end = owl_model.get_individuals_dic(recall_owl_path)
literature_dic, literature_ind_start, literature_ind_end = owl_model.get_individuals_dic(literature_owl_path)

# entities list
# print("document_owl实体数量:{}，集合:{}\n".format(len(document_dic.keys()),document_dic.keys()))
# print("event_owl实体数量:{}，集合:{}\n".format(len(event_dic.keys()),event_dic.keys()))
# print("personage_owl实体数量:{}，集合:{}\n".format(len(personage_dic.keys()),personage_dic.keys()))
# print("location_owl实体数量:{}，集合:{}\n".format(len(location_dic.keys()),location_dic.keys()))
# print("time_owl实体数量:{}，集合:{}\n".format(len(time_dic.keys()),time_dic.keys()))
# print("relic_owl实体数量:{}，集合:{}\n".format(len(relic_dic.keys()),relic_dic.keys()))
# print("recall_owl实体数量:{}，集合:{}\n".format(len(recall_dic.keys()),recall_dic.keys()))
# print("literature_owl实体数量:{}，集合:{}\n".format(len(literature_dic.keys()),literature_dic.keys()))

i = 0
print("开始爬取")
print("time_owl实体数量:{}，集合:{}\n".format(len(list(time_dic.keys())[-15000:]),list(time_dic.keys())[-15000:]))
for time in list(time_dic.keys())[-15000:]:
    try:
        dataDic = query(time[1:])
    except:
        print("{}无法爬取".format(time))
        pass
    if(len(dataDic["introduction"]) > 0 ):
        transfer2JSON(time, dataDic)
    else:
        print("无效json~")
        pass
    i+=1
    print("还有{}个实体待爬取".format(len(list(time_dic.keys())[-15000:])-i))

# locationList = []
# for location in location_dic.keys():
#     if(location in event_dic.keys()):
#         pass
#     elif(location in document_dic.keys()):
#         pass
#     elif (location in personage_dic.keys()):
#         pass
#     elif (location in relic_dic.keys()):
#         pass
#     else:
#         locationList.append(location)
# print(len(locationList))
# i = 0
# for location in locationList:
#     dataDic = query(location)
#     transfer2JSON(location, dataDic)
#     i+=1
#     print("还有{}个实体待爬取".format(len(locationList)-i))

    # dataDic = query(person)
    # transfer2JSON(person, dataDic)

    # 15000

# for document in document_dic.keys():
#     dataDic = query(document)
#     transfer2JSON(document, dataDic)

# for event in event_dic.keys():
#     dataDic = query(event)
#     transfer2JSON(event, dataDic)

# i = 960
# for person in list(personage_dic.keys())[960:]:
#     dataDic = query(person)
#     transfer2JSON(person, dataDic)
#     i+=1
#     print("第{}个实体抽取完毕".format(i))

